--
-- Author: ping.wang
-- Date: 2018-07-11 14:03:32
--
-- AuctionBidSellRender
--

local ClassRef = app.mvc.createRenderClass(...)
local string_format = string.format
local math_floor = math.floor
local auctionAdjustPriceRate = GD:getSystemPar( "auctionAdjustPriceRate",1)
local auctionSellPriceRateLimite = GD:getSystemPar( "auctionSellPriceRateLimite",20)
--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.AuctionSell2Render))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end

function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    self:setContentSize( self._ui.Panel:getContentSize() )

    local startPriceMinusBtn = self._ui.Panel_supplies_1:findChild("Button_minus")
    local startPricePlusBtn = self._ui.Panel_supplies_1:findChild("Button_plus")
    startPriceMinusBtn:addTouchEventListener(handler( self, self._minusStartPriceBtnClickEvent ) )
    startPricePlusBtn:addTouchEventListener(handler( self, self._plusStartPriceBtnClickEvent ) )

    local fixedPriceMinusBtn = self._ui.Panel_supplies:findChild("Button_minus")
    local fixedPricePlusBtn = self._ui.Panel_supplies:findChild("Button_plus")
    fixedPriceMinusBtn:addTouchEventListener(handler( self, self._minusFixedPriceClickEvent ) )
    fixedPricePlusBtn:addTouchEventListener(handler( self, self._plusFixedPriceClickEvent ) )

    ----------小键盘start--------------
    local suppliesPanelBg = self._ui.Panel_supplies:findChild("bg")
    display.uiAddClick( suppliesPanelBg, function()
        local btn = self._ui.Panel_supplies:findChild("Button_plus")
        local posTemp = btn:convertToWorldSpace(cc.p(0,0))
        local width, height = btn:getContentSize2()
        posTemp.x = posTemp.x + width

        executeFunc(self._onClick, "showMiniKeyboard", {posTemp,self._fixedPrice,handler( self, self.setAndGetFixedPrice )} )
    end )

    local suppliesPanelBg1 = self._ui.Panel_supplies_1:findChild("bg")
    display.uiAddClick( suppliesPanelBg1, function()
        local btn = self._ui.Panel_supplies_1:findChild("Button_plus")
        local posTemp = btn:convertToWorldSpace(cc.p(0,0))
        local width, height = btn:getContentSize2()
        posTemp.x = posTemp.x + width

        executeFunc(self._onClick, "showMiniKeyboard", {posTemp,self._startPrice,handler( self, self.setAndGetStartPrice )} )
    end )

    ----------小键盘end----------------

    self._ui.btn_sell:addClickEventListener(handler( self, self._sendSell ) )

    self._startPriceText = self._ui.Panel_supplies_1:findChild("num/num")
    self._fixedPriceText = self._ui.Panel_supplies:findChild("num/num")

    self._showTipsFlag = false
    self._startPrice = 0 --起拍价
    self._fixedPrice = 0 --一口价

    --设置货币icon
    local goldIcon1 = self._ui.Panel_supplies_1:findChild("num/gold")
    local goldIcon2 = self._ui.Panel_supplies:findChild("num/gold")

    local coinId = 99
    local publisher = app:getInnerPublisher()
    if publisher == Enums.Publisher.tencent then
        coinId = tonumber(GD:getSystemPar( "auctionBiddingGoodsLootType_tencent",99))
    else
        coinId = tonumber(GD:getSystemPar( "auctionBiddingGoodsLootType",115))
    end

    local coinConfig = GD:getCoinConfig(coinId)
    local iconPath = string.format("%s%s.png",Res.coinIconPath,coinConfig.bigicon)
    goldIcon1:loadTexture(iconPath)
    goldIcon2:loadTexture(iconPath)

    self:refresh()
end

function ClassRef:refresh()
    if self._data then
        self:setVisible( true )

        local data = self._data
        self._itemData = data.itemData
        --起拍价 服务器若没有发就读表 起拍价可以调整 上下浮动一定值 但是不会低于最低售价 不会高于最高售价
        --获取商品的指导售价 若获取不到则使用表中的数据

        if self._itemData.itemBagType == Enums.ItemBagType.equip then
            self._startPrice = self._itemData:getSuggestPrice()
        else
            if not data.suggestPrice then
                self._startPrice = self._itemData:getSuggestPrice()
            else
                self._startPrice = data.suggestPrice
            end
        end

        self._originalStartPrice = self._startPrice --记录一个初始值

        --一口价最高不可高过表中的最高售价 最低不可低于起拍价
        self:setAndGetFixedPrice(self._itemData:getMaxSoldPrice())
        self:setAndGetStartPrice(self._startPrice)

        --按比例算出每次增加和减少时候的值
        self._preValForStartPrice = math_floor(auctionAdjustPriceRate * self._startPrice/100)
        if self._preValForStartPrice == 0 then
            self._preValForStartPrice = 1
        end
        self._preValForFixedPrice = math_floor(auctionAdjustPriceRate * self._fixedPrice/100)
        if self._preValForFixedPrice == 0 then
            self._preValForFixedPrice = 1
        end

        self._ui["Panel/tips"]:setString(L("auction_bidtax"))

    else
        self:setVisible( false )
    end
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

--达到上限的时候显示一次tips
function ClassRef:_showTipsByPrecent(str)
    if self._showTipsFlag then
        display.pushToast(str)
        self._showTipsFlag = false
    end
end

--设置一口价
function ClassRef:setAndGetFixedPrice(num)
    num = math_floor(num)

    local suggestMaxPrice = self._itemData:getMaxSoldPrice()

    if num <= self._startPrice then
        num = self._startPrice
        self:_showTipsByPrecent(L("auction_tips3"))
    elseif num >= suggestMaxPrice then
        num = suggestMaxPrice
    end

    self._fixedPrice = num
    self._fixedPriceText:setString(string_format(L("auction_donePrice"),self._fixedPrice))

    return self._fixedPrice
end

--设置起始价
function ClassRef:setAndGetStartPrice(num)
    num = math_floor(num)
    if num > self._fixedPrice then
        num = self._fixedPrice
        self:_showTipsByPrecent(L("auction_tips1"))
    end

    --单价为推荐价格的%20左右的浮动 且 >= 1
    local minPrice = self._itemData:getMinSoldPrice()
    --local minPrice = math_floor(self._originalStartPrice * (100 - auctionSellPriceRateLimite) / 100)
    local maxPrice = math_floor(self._originalStartPrice * (100 + auctionSellPriceRateLimite) / 100)

    local suggestMinPrice = self._itemData:getMinSoldPrice()
    local suggestMaxPrice = self._itemData:getMaxSoldPrice()
    local tipsStr = string_format(L("auction_tips6"),auctionSellPriceRateLimite)
    if num < minPrice then
        self._startPrice = minPrice
        self:_showTipsByPrecent(L("auction_tips13"))
    elseif num > maxPrice then
        self._startPrice = maxPrice
        self:_showTipsByPrecent(tipsStr)
    else
        self._startPrice = num
    end

    if self._startPrice < suggestMinPrice then
        self._startPrice = suggestMinPrice
        self:_showTipsByPrecent(L("action_minprice_notice"))
    elseif self._startPrice > suggestMaxPrice then
        self._startPrice = suggestMaxPrice
        self:_showTipsByPrecent(L("action_maxprice_notice"))
    end

    self._startPriceText:setString(string_format(L("auction_firstPrice"),self._startPrice))
    return self._startPrice
end

--减少起拍价
function ClassRef:_minusStartPriceBtnClickEvent(sender,eventType)
    self:_doLongPressClickEvent(sender,eventType,function() self:_minusStartPrice() end)
end

--增大起拍价
function ClassRef:_plusStartPriceBtnClickEvent(sender,eventType)
    self:_doLongPressClickEvent(sender,eventType,function() self:_plusStartPrice() end)
end

--按比率增加和减少起拍价
function ClassRef:_minusStartPrice()
    self:setAndGetStartPrice(self._startPrice - self._preValForStartPrice)
end

function ClassRef:_plusStartPrice()
    self:setAndGetStartPrice(self._startPrice + self._preValForStartPrice)
end

--减少一口价
function ClassRef:_minusFixedPriceClickEvent(sender,eventType)
    self:_doLongPressClickEvent(sender,eventType,function() self:_minusFixedPrice() end)
end

--增大一口价
function ClassRef:_plusFixedPriceClickEvent(sender,eventType)
    self:_doLongPressClickEvent(sender,eventType,function() self:_plusFixedPrice() end)
end

--按比率增加和减少一口价
function ClassRef:_minusFixedPrice()
    self:setAndGetFixedPrice(self._fixedPrice - self._preValForFixedPrice)
end

function ClassRef:_plusFixedPrice()
    self:setAndGetFixedPrice(self._fixedPrice + self._preValForFixedPrice)
end

function ClassRef:_doLongPressClickEvent(sender,eventType,callFunc)
    if eventType == ccui.TouchEventType.began then
        self._showTipsFlag = true
        self._delayPressAction = self:performWithDelay(
            function()
                if not self._longPressAction then
                    self._longPressAction = self:schedule(callFunc,0.05)
                end
            end,0.3) --0.3秒后执行回调

    elseif eventType == ccui.TouchEventType.ended or eventType == ccui.TouchEventType.canceled then

        if self._delayPressAction then
            self:stopAction(self._delayPressAction)
            self._delayPressAction = nil
        end

        if self._longPressAction then
            self:stopAction(self._longPressAction)
            self._longPressAction = nil
        else
            callFunc()
        end
        self._showTipsFlag = false
    end
end

function ClassRef:_sendSell()
    --参数上需要物品UID 起拍价 一口价
    --竞价商品只能一个个的卖
    executeFunc(self._onClick, "bidSell",{self._itemData:getUid(),self._startPrice,1,self._fixedPrice})
end

return ClassRef
